<?php
/**
 * @file stardefine.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2016-1-4 下午1:53:37 $
 * @brief  $达人
 */
namespace haibao\cms\business\star;

use haibao\cms\data\star\StarDefine as StarDefineData;
use haibao\cms\model\data\star\StarDefine as StarDefineModel;
use haibao\app\business\BusinessException;

class StarDefine extends \haibao\cms\business\Base{
	
	public static $starIdentity = array(
		StarDefineModel::IDENTITY_BLOGGER =>'博主',
		StarDefineModel::IDENTITY_NETHOT =>'网红',
		StarDefineModel::IDENTITY_LIFT_ARTIST =>'生活艺术家',
	);
    /**
     * 美妆博主、时尚博主、网红类、生活类、自媒体、视频直播类、旅行类、健身类
     */
	public static $starCategory = array(
		StarDefineModel::CATEGORY_MAKEUP =>'美妆博主',
		StarDefineModel::CATEGORY_FASHION =>'时尚博主',
		StarDefineModel::CATEGORY_FASHION_MAKEUP =>'时尚美妆类',
		StarDefineModel::CATEGORY_LIFE =>'生活类',
        StarDefineModel::CATEGORY_MODEL =>'模特类',
        StarDefineModel::CATEGORY_WANGHONG =>'网红类',
        StarDefineModel::CATEGORY_ZIMEITI =>'自媒体',
        StarDefineModel::CATEGORY_SHIPINZHIBO =>'视频直播类',
        StarDefineModel::CATEGORY_LVXING =>'旅行类',
        StarDefineModel::CATEGORY_JIANSHEN =>'健身类',
	);
	
	public static $starCooperateWilling = array(
		StarDefineModel::WILLINGNESS_HIGH =>'配合度高',
		StarDefineModel::REQUIRE_HIGH =>'对方要求高',
		StarDefineModel::WILLINGNESS_LOW =>'配合度低',
		StarDefineModel::NOT_RECOMMENDED_COOPERATE =>'不建议合作',
	);
	
	public static $starBrandSatisfaction = array(
		StarDefineModel::BRAND_SATISFACTION_EXCELLENT =>'优质',
		StarDefineModel::BRAND_SATISFACTION_GOOD =>'良好',
		StarDefineModel::BRAND_SATISFACTION_DEPRECATED =>'不推荐',
	    StarDefineModel::BRAND_SATISFACTION_NORMAL =>'一般',
	);
    /**
     * 美拍、一直播、秒拍、小影、快手、小红书、抹茶、Instagram、Nice、淘宝、Keep、穷游、蚂蜂窝
     */
	public static $starPlatForm = array(
		StarDefineModel::PLAT_FORM_MEIPAI => '美拍',
        StarDefineModel::PLAT_FORM_HONGSHU => '小红书',
        StarDefineModel::PLAT_FORM_YIZHIBO => '一直播',
        StarDefineModel::PLAT_FORM_MIAOPAI => '秒拍',
        StarDefineModel::PLAT_FORM_XIAOYING => '小影',
        StarDefineModel::PLAT_FORM_KUAISHOU => '快手',
        StarDefineModel::PLAT_FORM_MOCHA => '抹茶',
        StarDefineModel::PLAT_FORM_INSTAGRAM => 'Instagram',
        StarDefineModel::PLAT_FORM_NICE => 'Nice',
        StarDefineModel::PLAT_FORM_TAOBAO => '淘宝',
        StarDefineModel::PLAT_FORM_KEEP => 'Keep',
        StarDefineModel::PLAT_FORM_QIONGYOU => '穷游',
        StarDefineModel::PLAT_FORM_MAFENGWO => '蚂蜂窝',

	);
	
	public static $starAccountType = array(
		StarDefineModel::STAR_ACCOUNT_WEIBO => '微博',
		StarDefineModel::STAR_ACCOUNT_WEIXIN => '微信',
		StarDefineModel::STAR_ACCOUNT_BLOG => '博客',
	    StarDefineModel::STAR_ACCOUNT_WEIXIN_PUBLIC => '微信公众号',
	);
	
	public static function getStarDefineInfoById($starId){
		$starDefineData = new StarDefineData();
		return $starDefineData->getStarDefineInfoById($starId);
	}
	
	
	/**
	 * 获取达人列表
	 */
	public static function getStarDefineList($query){
		$starDefineData = new StarDefineData();
		return $starDefineData->getAll($query);
	}
	
	public static function getOneStarDefine($query){
		$starDefineData = new StarDefineData();
		return $starDefineData->getOne($query);
	}
	
	public static function getAllStarDefine($query){
		$starDefineData = new StarDefineData();
		return $starDefineData->getAll($query);
	}
	
	public static function getOneStarCooperateById($starId,$type){
		$starDefineData = new StarDefineData();
		return $starDefineData->getOneStarCooperateById($starId,$type);
	}
	
	public static function getAllStarId(){
	    $starDefineData = new StarDefineData();
	    return $starDefineData->getAllStarId();
	}
	
	public static function getStarInfoByIds($ids){
		$starDefineData = new StarDefineData();
		return $starDefineData->getStarByIds($ids);
	}
	
	/**
	 * 删除达人
	 */
	public static function delStarDefine($id,$status){
		self::checkOperation(self::FUNC_CODE_STAR_DELETE);
		$starDefineData = new StarDefineData();
		$starDefineData->delStarDefine($id, $status);
		self::saveOperateHistory(self::FUNC_CODE_STAR_DELETE, $status);
	}
	
	/**
	 * 添加修改达人信息
	 */
	public static function saveStarInfo($model,$secretModel,$brokerModel,$starCooperateArray,$starPropertyArray,$newPlatInfoArray,$propertyWModel){
		if(!$model->Id){
			self::checkOperation(self::FUNC_CODE_STAR_ADD);
			self::checkForm($model,$secretModel,$brokerModel,$starCooperateArray,$starPropertyArray);
			if(isset($model->RealName) && $model->RealName){
				$starDefineData = new StarDefineData();
				$nameResult = $starDefineData->getOneByRealName($model->RealName);
				if($nameResult){
					throw new BusinessException('博主基本信息中，名字已存在');
				}
			}
			$model->CreateBy = self::getCurrentUser()->Id;
			$model->CreateTime  = new \DateTime();
			$model->UpdateTime = new \DateTime();
		}else {
			self::checkOperation(self::FUNC_CODE_STAR_EDIT);
			self::checkForm($model,$secretModel,$brokerModel,$starCooperateArray,$starPropertyArray);
			if(isset($model->RealName) && $model->RealName && $model->Id){
				$starDefineData = new StarDefineData();
				$nameResult = $starDefineData->getOneByRealName($model->RealName,$model->Id);
				if($nameResult){
					throw new BusinessException('博主基本信息中，名字已存在');
				}
			}
			$starInfo = self::getOneStarInfoById($model->Id);
			$model->CreateBy = $starInfo->CreateBy;
			$model->CreateTime = $starInfo->CreateTime;
			$model->UpdateTime = date('Y-m-d H:i:s');
		}
		$starDefineData = new StarDefineData();
		$starDefineData->saveStarInfo($model,$secretModel,$brokerModel,$starCooperateArray,$starPropertyArray,$newPlatInfoArray,$propertyWModel);
		self::saveOperateHistory(self::FUNC_CODE_STAR_EDIT, $model->toArray(true,true));
	}
	
	public static function getOneStarInfoById($id){
		$starDefineData = new StarDefineData();
		return $starDefineData->getOneById($id);
	}
	
	/**
	 * 表单验证
	 */
	private static function checkForm($model,$secretModel,$brokerModel,$starCooperateArray,$starPropertyArray){

		if(isset($secretModel->Phone) && $secretModel->Phone){
			if(!preg_match("/1[3456789]{1}\d{9}$/",$secretModel->Phone)){
				throw new BusinessException('博主保密信息中，请输入正确的手机格式');
			}
		}
		if(isset($secretModel->Email) && $secretModel->Email){
			if(!preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$secretModel->Email)){
				throw new BusinessException('博主保密信息中，请输入正确的邮箱格式');
			}
		}
		if(isset($brokerModel->Phone) && $brokerModel->Phone){
			if(!preg_match("/1[3458]{1}\d{9}$/",$brokerModel->Phone)){
				throw new BusinessException('经纪人信息中，请输入正确的手机格式');
			}
		}
		if(isset($brokerModel->Email) && $brokerModel->Email){
			if(!preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$brokerModel->Email)){
				throw new BusinessException('经纪人信息中，请输入正确的邮箱格式');
			}
		}
		if($starCooperateArray){
			foreach($starCooperateArray as $key=>$val){
				if(!$val->CooperateBrand){
					throw new BusinessException('请输入品牌名称');
				}
				if(!$val->CooperateTime){
					throw new BusinessException('请输入合作时间');
				}
			}
		}
		if($starPropertyArray){
			foreach($starPropertyArray as $key=>$val){
				if(!$val->StarAccountType){
					throw new BusinessException('请选择相关信息类型');
				}
			}
		}
		
	}

	public static function saveOneStar($model){
		$model->CreateBy = self::getCurrentUser()->Id;
		$starDefineData = new StarDefineData();
		$starDefineData->add($model);
		return $model->Id;
	}
	
	

}